package com.gz.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataFmtUtil {
    private static Logger logger = LoggerFactory.getLogger(DataFmtUtil.class);

    /**
     * @param str
     * @return java.lang.String
     * @author JiHaokun
     * @Description 将下划线转为驼峰
     * @Date 2022/7/22 17:04
     * @since 1.0.0
     */
    public static String xX2x_x(String str) {
        Pattern compile = Pattern.compile("[A-Z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static Object getFieldValueByName(String fieldName, Object o) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = o.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(o, new Object[]{});
            return value;
        } catch (Exception e) {
            logger.error("获取属性值失败！" + e, e);
        }
        return null;
    }

    public static String[] getFiledName(Object o) {
        Field[] fields = o.getClass().getDeclaredFields();
        String[] fieldNames = new String[fields.length];
        for (int i = 0; i < fields.length; i++) {
            fieldNames[i] = fields[i].getName();
        }
        return fieldNames;
    }

    public static long calDistanceDay(String day) throws ParseException {
        /** 2.DateFormat类中的parse方法把字符串day解析为Date格式。 */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        /** 到这一步可能有异常直接alt+回车抛出去，一直抛直到没有报错留给虚拟机处理。*/
        Date targerDay = sdf.parse(day);//这句的作用是解析为文本
        /**3.把Date格式的出生日期转换为毫秒值用Date类的getTime();方法*/
        long targetTime = targerDay.getTime();
       /** 4.获取当前日期，转换为毫秒值getTime();方法*/
        long todaytime = new Date().getTime();
        long time =  targetTime - todaytime;
        /** 用format()方法将一个 Date 格式化为日期/时间字符串。 返回一个字符串。*/
        String today = sdf.format(new Date());
        return time / 1000 / 60 / 60 / 24;
    }
    public static Date getDayAf120() throws ParseException {
        // 获取当前日期和时间
        LocalDateTime currentTime = LocalDateTime.now();

        // 计算120天后的日期和时间
        LocalDateTime futureDateTime = currentTime.plusDays(120);

        // 格式化日期时间为字符串（可选）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedCurrentTime = currentTime.format(formatter);
        String formattedFutureDateTime = futureDateTime.format(formatter);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(formattedFutureDateTime);
    }


}
